#include "kdcsubtitlewidget.h"

#include <QTableView>
#include <KLocale>
#include <QHeaderView>
#include <KIcon>
#include <KPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <KButtonGroup>
#include <QFormLayout>
#include <KFileDialog>
#include <KUrl>
#include <KConfig>
#include <KConfigGroup>

#include "kdcsubtitlesconfig.h"
#include "kdcsubtitledelegate.h"
#include <subtitle.h>

KDCSubtitleWidget::KDCSubtitleWidget( QObject *aParent ) : QWidget( 0 ), pRow( -1 ), pMovie( NULL )
{
	//setParent( aParent );
	
	QHBoxLayout *mainLayout = new QHBoxLayout;
	
	pModel = new KDCSubtitleModel( this );
	connect( pModel, SIGNAL( beginTitleRowsChanged() ), this, SLOT( slotBeginTitleRowsChanged() ) );
	connect( pModel, SIGNAL( endTitleRowsChanged() ), this, SLOT( slotEndTitleRowsChanged() ) );
	pView = new QTableView( this );
	pView->setDragEnabled( true );
	pView->setAcceptDrops( true );
	pView->setDropIndicatorShown( true );
	pView->setModel( pModel );
	pView->verticalHeader()->hide();
	pView->horizontalHeader()->setStretchLastSection( true );
	pView->setShowGrid( false );
	pView->verticalHeader()->setDefaultSectionSize( 20 );
	pView->horizontalHeader()->setHighlightSections( false );
	pView->setSelectionBehavior( QAbstractItemView::SelectRows );
	pView->setItemDelegate( new KDCSubtitleDelegate( this ) );
	connect( pView->selectionModel(), SIGNAL( currentRowChanged( const QModelIndex &, const QModelIndex & ) ), this, SLOT( slotCurrentRowChanged( const QModelIndex &, const QModelIndex & ) ) );
	
	QHBoxLayout *hLayout = new QHBoxLayout;
	pAddBtn = new KPushButton( KIcon("document-open"), i18n("Add"), this );
	connect( pAddBtn, SIGNAL( clicked() ), this, SLOT( add() ) );
	hLayout->addWidget( pAddBtn );
	
	pDelBtn = new KPushButton( KIcon("document-close"), i18n("Remove"), this );
	connect( pDelBtn, SIGNAL( clicked() ), this, SLOT( remove() ) );
	hLayout->addWidget( pDelBtn );
	hLayout->addStretch();
	
	QVBoxLayout *vLayout = new QVBoxLayout;
	vLayout->addLayout( hLayout );
	vLayout->addWidget( pView );
	mainLayout->addLayout( vLayout, 2 );
	
	vLayout = new QVBoxLayout;
	KButtonGroup *group = new KButtonGroup( this );
	group->setTitle( i18n("Options") );
	QFormLayout *fLayout = new QFormLayout( group );
	hLayout = new QHBoxLayout;
	hBtnGroup = new QButtonGroup( this );
	hBtnGroup->setExclusive( true );
	connect( hBtnGroup, SIGNAL( buttonPressed( int ) ), this, SLOT( horizontalButtonPressed( int ) ) );
	KPushButton *btn = new KPushButton( KIcon("align-horizontal-left"), "", this );
	btn->setCheckable( true );
	hLayout->addWidget( btn );
	hBtnGroup->addButton( btn, Qt::AlignLeft );
	btn = new KPushButton( KIcon("align-horizontal-center"), "", this );
	btn->setCheckable( true );
	hLayout->addWidget( btn );
	hBtnGroup->addButton( btn, Qt::AlignHCenter );
	btn = new KPushButton( KIcon("align-horizontal-right"), "", this );
	btn->setCheckable( true );
	hLayout->addWidget( btn );
	hBtnGroup->addButton( btn, Qt::AlignRight );
	hLayout->addStretch();
	fLayout->addRow( i18n("Horizontal alignment:"), hLayout );
	vBtnGroup = new QButtonGroup( this );
	vBtnGroup->setExclusive( true );
	connect( vBtnGroup, SIGNAL( buttonPressed( int ) ), this, SLOT( verticalButtonPressed( int ) ) );
	hLayout = new QHBoxLayout;
	btn = new KPushButton( KIcon("align-vertical-top"), "", this );
	btn->setCheckable( true );
	hLayout->addWidget( btn );
	vBtnGroup->addButton( btn, Qt::AlignTop );
	btn = new KPushButton( KIcon("align-vertical-center"), "", this );
	btn->setCheckable( true );
	hLayout->addWidget( btn );
	vBtnGroup->addButton( btn, Qt::AlignVCenter );
	btn = new KPushButton( KIcon("align-vertical-bottom"), "", this );
	btn->setCheckable( true );
	hLayout->addWidget( btn );
	vBtnGroup->addButton( btn, Qt::AlignBottom );
	hLayout->addStretch();
	fLayout->addRow( i18n("Vertical alignment:"), hLayout );
	pSize = new KIntNumInput( this );
	connect( pSize, SIGNAL( valueChanged( int ) ), this, SLOT( setSize( int ) ) );
	fLayout->addRow( i18n("Size:"), pSize );

	vLayout->addWidget( group );
	vLayout->addStretch();
	mainLayout->addLayout( vLayout );
	mainLayout->addStretch();
	setLayout( mainLayout );
}

KDCSubtitleWidget::~KDCSubtitleWidget()
{

}

void KDCSubtitleWidget::setMovie( KDCMovie *aMovie )
{
	slotBeginTitleRowsChanged();
	pMovie = aMovie;
	pModel->setMovie( aMovie );
	if ( pMovie )
	{
		pSize->setValue( pMovie->subtitles()->size() );
		vBtnGroup->button( (int)pMovie->subtitles()->verticalAlignment() )->setChecked( true );
		hBtnGroup->button( (int)pMovie->subtitles()->horizontalAlignment() )->setChecked( true );
	}
	setup( pMovie );
	slotEndTitleRowsChanged();
}

void KDCSubtitleWidget::add()
{
	if ( !pMovie || pRow < 0 )
		return;
	KConfig config("kdvdcreator");
	KConfigGroup general( &config, "General" );
	QString dir = general.readEntry( "LastSubtitleDir", QDir::homePath() );
	QString filter( "*.srt *.sub *.txt|All Subtitle Files" );
	QStringList files = KFileDialog::getOpenFileNames( KUrl( dir ), filter, this, i18n("Select one or more subtitle files to open") );
	if ( !files.isEmpty() )
	{
		general.writeEntry( "LastSubtitleDir", QFileInfo( files.first() ).path() );
		general.config()->sync();
		KDCMovie *movie = pMovie;
		int idx = pModel->movieFromRow( pRow );
		if ( idx > 0 )
			movie = pMovie->subMovies().at( idx - 1 );
		Subtitle *sub;
		foreach( QString file, files )
		{
			sub = new Subtitle( file, movie->video()->metaData()->fps() );
			sub->setCodec( KDCSubtitleEditorConfig::codec() );
			pModel->addSubtitle( movie, sub );
		}
	}
}

void KDCSubtitleWidget::remove()
{
	if ( !pMovie || pRow < 0 )
		return;
	pModel->removeSubtitle( pRow );
}

void KDCSubtitleWidget::verticalButtonPressed( int aId )
{
	if ( pMovie )
		pMovie->subtitles()->setVerticalAlignment( (Qt::Alignment)aId );
}

void KDCSubtitleWidget::horizontalButtonPressed( int aId )
{
	if ( pMovie )
		pMovie->subtitles()->setHorizontalAlignment( (Qt::Alignment)aId );
}

void KDCSubtitleWidget::setSize( int aSize )
{
	if ( pMovie )
		pMovie->subtitles()->setSize( aSize );
}

void KDCSubtitleWidget::slotCurrentRowChanged( const QModelIndex &aCurrent, const QModelIndex &aPrevious )
{
	if ( aCurrent.isValid() )
	{
		pRow = aCurrent.row();
		pAddBtn->setEnabled( true );
		pDelBtn->setEnabled( pModel->movieTitleRow( pModel->movieFromRow( pRow ) ) != pRow );
	}
	else
	{
		pRow = -1;
		pAddBtn->setEnabled( false );
		pDelBtn->setEnabled( false );
	}
}

void KDCSubtitleWidget::setup( bool aEnabled )
{
	pAddBtn->setEnabled( aEnabled );
	pDelBtn->setEnabled( aEnabled );
	QAbstractButton *btn;
	foreach( btn, hBtnGroup->buttons() )
		btn->setEnabled( aEnabled );
	foreach( btn, vBtnGroup->buttons() )
		btn->setEnabled( aEnabled );
	pSize->setEnabled( aEnabled );
}

void KDCSubtitleWidget::slotEndTitleRowsChanged()
{
	QList<int> rows = pModel->titleRows();
	for ( int i=0; i<rows.size(); ++i )
		pView->setSpan( rows.at( i ), 0, 0, pModel->columnCount() );
}

void KDCSubtitleWidget::slotBeginTitleRowsChanged()
{
	QList<int> rows = pModel->titleRows();
	for ( int i=0; i<rows.size(); ++i )
		pView->setSpan( rows.at( i ), 0, 0, 0 );
}
